package com.xxx.car.redisCluster.config;

/**
 * Created by xiao.lai on 16/5/19.
 *
 * BinaryJedisCluster和JedisCluster,BinaryJedisCluster提供了byte[]字节数组的操作。方便序列化后直接传对象
 */

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.BinaryJedisCluster;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class JedisConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(JedisConfig.class);

    @Autowired
    private RedisProperties redisProperties;

    /**
     * 注意：
     * 这里返回的JedisCluster是单例的，并且可以直接注入到其他类中去使用
     * 建议使用jedis-2.8
     * @return
     */
//    @Bean
//    public BinaryJedisCluster getJedisCluster() {
//        String[] serverArray = new String[]{};
//        try {
//            serverArray = redisProperties.getClusterNodes().split(",");
//
//        }catch (Exception e) {
//            LOGGER.error("redisSentinel.cache.clusterNodes无法解析");
//        }
//        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
//
//        for (String ipPort : serverArray) {
//            String[] ipPortPair = ipPort.split(":");
//            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
//        }
//        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
//        config.setMinIdle(redisProperties.getMinIdle());
//        config.setMaxIdle(redisProperties.getMaxIdle());
//        config.setMaxTotal(redisProperties.getMaxActive());
//        config.setMaxIdle(redisProperties.getMaxIdle());
//        return new BinaryJedisCluster(nodes, redisProperties.getCommandTimeout(), redisProperties.getMaxRedirections(), config);
//    }

    @Bean
    public JedisPool getJedisPool() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMinIdle(redisProperties.getMinIdle());
        config.setMaxIdle(redisProperties.getMaxIdle());
        config.setMaxTotal(redisProperties.getMaxActive());
        config.setMaxIdle(redisProperties.getMaxIdle());
        return new JedisPool(config, "47.92.113.41", 6379, redisProperties.getCommandTimeout(), "ixSUNEgAAdXRN7vF0a", redisProperties.getPoolDb());
    }

}
